package com.hirisun.calendar.gantt;

import com.hirisun.calendar.time.ConversionRatioManager;

public class Duration {
	private String label;
	private long value;
	private char _unit = 'd';

	private ConversionRatioManager manager;

	public ConversionRatioManager getManager() {
		return manager;
	}

	public String getLabel() {
		return label;
	}

	public void setManager(ConversionRatioManager manager) {
		this.manager = manager;
	}

	public void setLabel(String label) {
		this.label = label;
		synchronize();
	}

	private void synchronize() {
		// labe;
		String _label = label;
		char[] units = _label.toCharArray();
		char c;
		StringBuffer sb = new StringBuffer();
		StringBuffer unit = new StringBuffer();
		float toal = 0;
		for (int i = 0; i < units.length; i++) {
			c = units[i];
			if (Character.isDigit(c) || c == '.') {
				sb.append(c);
			} else if (Character.isLetter(c)) {
				if (c != 'w' && c != 'd' && c != 'm' && c != 'f' && c != 'h') {
					error();
				}
				
				unit.append(c);
				float back_int = Float.valueOf(sb.toString()).floatValue();
				sb = new StringBuffer();
				toal += back_int * manager.getUnit(c);
			}
		}
		
		this._unit = unit.toString().toCharArray()[0];

		value = (long) toal;
	}

	private void error() {
		throw new RuntimeException();

	}

	public void setValue(long value) {
		this.value = value;				
		float temp;		
		temp = ((float)value)/ manager.getUnit(this._unit);
		
		this.label = temp + "" + this._unit;
	}

	public long getValue() {
		return value;
	}

}
